\n
)for i in $(seq 1 20); do
it_no=$((50*$i))
sed -i "${line_no}ait_no = ${it_no}" $py_fname
sed -i "${line_no}d" $py_fname
echo "Running it = "$it_no
pvpython $py_fname¶
done¶
str=""
if [ "$str" == "" ];then
echo empty
elif [ "$str" == " " ];then
echo space
elif [ ! "$str" ];then
echo NULL
fi
if [ ! "$str" ];then
echo NULL
fi
$ ./test.sh
empty
NULL
#!/bin/bash
STR='GNU/Linux is an operating system'
SUB='Linux'
if [[ "$STR" == *"$SUB"* ]]; then
echo "It's there."
fi
Sometimes I need to decrease the heading level in a Markdown file. For example, the following markdown texts
## Heading 1
A sentence.
### Heading 1.1
Another sentence
need to be converted to be as follows,
# Heading 1
A sentence.
## Heading 1.1
Another sentence
By using sed, we could finish this task by
sed -i 's/^#//g' file.md
todate=$(date -d 2013-07-18 +%s)
cond=$(date -d 2014-08-19 +%s)
if [ $todate -ge $cond ];
then
break
fi
ids=(aa ab aa ac aa ad) && echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '
Generating random number between 1 and 10
$(( ( RANDOM % 10 ) + 1 ))
echo "ref12345678"|grep -o '[0-9]\+'
or
echo "ref12345678"| awk -F'[^0-9]*' '{print $2}'
temp="${opt%\"}"
temp="${temp#\"}"
echo "$temp"
${opt%\"}
will remove the suffix "
(escaped
with a backslash to prevent shell interpretation).
${temp#\"}
will remove the prefix "
(escaped with a backslash to prevent shell interpretation).
I have a string like AxxBCyyyDEFzzLMN
and I want to
replace all the occurrences of x, y, z
with
_
.
echo "$string" | tr xyz _
would replace each occurrence of x, y, z
with
_
, giving A__BC___DEF__LMN
in your
example.
echo "$string" | sed -r 's/[xyz]+/_/g'
would replace repeating occurrences of x, y, z
with a
single _
, giving A_BC_DEF_LMN
in your
example.
$ cat test
asdbl erwbniu
assdbl ebniu
asdbl1 erwbni
3asdbl erwniu
# Replace all occurences of asdbl in the text file with NEWTC
$ sed 's/asdbl/NEWTC/g' test
NEWTC erwbniu
assdbl ebniu
NEWTC1 erwbni
3NEWTC erwniu
# Replace all lines containing asdbl in the text file with NEWTC
$ sed 's/.*asdbl.*/NEWTC/g' test
NEWTC
assdbl ebniu
NEWTC
NEWTC
# Exact match of asdbl
$ sed 's/\<asdbl\>/NEWTC/g' test
NEWTC erwbniu
assdbl ebniu
asdbl1 erwbni
3asdbl erwniu
Use -i
to do the in-place modification.
for i in 1 2 3; do
echo "$i"
done
or
listVar="1 2 3"
for i in $listVar; do
echo "$i"
done
$ NUMBER=$(echo "I am 999 years old." | sed 's/[^0-9]*//g')
$ echo $NUMBER
999
Or
$ STRING="I am 999 years old."
$ echo "${STRING//[!0-9]/}"
999
OR
$ echo "${STRING//[^0-9]/}"
Number sequences starting with a 0
are interpreted as
octal numbers, i.e. base 8
.
$ cat test.sh
var="00011"
echo "The input is $var"
printf "The number with leading zeros is defaultly treated as octals: %d\n" $var
var=$((10#$var))
printf "Convert the octals to the number of base 10: %d\n" $var
$ ./test.sh
The input is 00011
The number with leading zeros is defaultly treated as octals: 9
Convert the octals to the number of base 10: 11
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
Use these ANSI escape codes:
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"
echo -e "I ${RED}love${NC} Stack Overflow"
Use xargs
in the pipe.
$ echo " Bash Scripting Language " | xargs
Bash Scripting Language
POSIX standard methods are as follows,
Use tr
$ echo "$a" | tr '[:upper:]' '[:lower:]'
hi all
Use awk
$ echo "$a" | awk '{print tolower($0)}'
hi all
\n
)printf "$text_content"
sed 's/\x1b\[[0-9;]*m//g'
\x1b
(or \x1B
) is the escape special
character (sed
does not support alternatives
\e
and \033
)\[
is the second character of the escape sequence[0-9;]*
is the color value(s) regexm
is the last character of the escape sequence$ cat test.sh
#!/bin/bash
echo "pwd: `pwd`"
echo "\$0: $0"
echo "basename: `basename $0`"
echo "dirname: `dirname $0`"
echo "dirname/readlink: $(dirname $(readlink -f $0))"
$ ./test.sh
pwd: /home/data/jcshi
$0: ./test.sh
basename: test.sh
dirname: .
dirname/readlink: /home/data/jcshi
readlink
will resolve the script path to an absolute
path from the root of the filesystemUse exit
or return
under different running
modes ./test.sh
, source ./test.sh
and
. ./test.sh
.
./test.sh
opens a sub-terminal while the sourced running
mode source ./test.sh
and . ./test.sh
runs in
the current terminal.
$ cat test.sh
echo $?
drga
ierr=$?
if [ "$ierr" = '127' ]; then
echo "FAIL"
exit -1
# return
fi
echo "PASS"
$ ./test.sh
0
./test.sh: line 2: drga: command not found
FAIL
$ source ./test.sh
# This command exits the terminal
$ . ./test.sh
# This command exits the terminal
$ cat test.sh
echo $?
drga
ierr=$?
if [ "$ierr" = '127' ]; then
echo "FAIL"
# exit -1
return
fi
echo "PASS"
$ ./test.sh
0
./test.sh: line 2: drga: command not found
FAIL
./test.sh: line 7: return: can only `return' from a function or sourced script
PASS
$ source ./test.sh
0
./test.sh:2: command not found: drga
FAIL
$ . ./test.sh
127
./test.sh:2: command not found: drga
FAIL
Use the set -e
builtin:
#!/bin/bash
set -e
# Any subsequent(*) commands which fail will cause the shell script to exit immediately
Alternatively, you can pass -e
on the command line:
bash -e my_script.sh
Use the following script as the example.
echo $?
# This command is not a valid command. Thus it throws stderr.
drga
echo $?
$ ./test.sh >out 2>err
$ cat out
0
127
$ cat err
./test.sh: line 2: drga: command not found
>out
), and then redirect
stderr to stdout (2>&1
):$ ./test.sh >out 2>&1
$ cat out
0
./test.sh: line 2: drga: command not found
127
$ ./test.sh &>out
$ cat out
0
./test.sh: line 2: drga: command not found
127
First redirect stderr to stdout — the pipe; then redirect stdout to
/dev/null
(without changing where stderr is going):
$ ./test.sh 2>&1 >/dev/null
./test.sh: line 2: drga: command not found
Note the difference between this method and the 2nd point.
For the details of I/O redirection in all its variety, see the chapter on Redirections in the Bash reference manual.
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
if [ "$source_file" -nt "$target_file" ]
then
printf '%s\n' "$source_file is newer than $target_file"
fi
line_no=$(grep -n 'it_no = ' $py_fname | awk -F':' '{print $1}')
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH
if ! type "$foobar_command_name" > /dev/null; then
# foobar_command_name does not exit
fi
cd $HOME
does not take effect if you do
./test.sh
. You should do as follows,
source ./test.sh# or. ./test.sh
nohup
and
write its pid to a filenohup ./myprogram.sh > /dev/null 2>&1 & echo $! > run.pid
-bash: warning: setlocale:
Warning is something like
-bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
You need to generate the file.
[root@centos6-vm01 ~]# vim /etc/environment #添加下面两行内容
LANG="en_US.UTF-8"
LC_ALL=
[root@centos6-vm01 ~]# source /etc/environment
[root@centos6-vm01 ~]# localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
You may need to install localedef
.
shopt
is a shell builtin command to set and unset
(remove) various Bash shell options. To see current settings, type
shopt
.
To enable (set) option use the following command:
shopt -s optionNameHere
To disable (unset) option use the following command:
shopt -u optionNameHere
if [ -z "$PS1" ]; then
echo This shell is not interactive
else
echo This shell is interactive
fi
Reference: 6.3.2 Is this Shell Interactive?
# jcshi @ master in ~ [13:03:51]
$ awk '{s+=$1} END {print s}' tmp.dat
10
# jcshi @ master in ~ [13:04:25]
$ cat tmp.dat
1
2
3
4
Use bc
program.
if (( $(echo "$num1 > $num2" |bc -l) )); then
...
fi